use common_base::{prelude::*, serde};

#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub enum DataStatus {
    Normal,
    Update,
    Insert,
    Delete,
    /// 子数据集有修改
    Child,
}

impl TryFrom<i32> for DataStatus {
    type Error = ErrorCode;

    fn try_from(value: i32) -> std::result::Result<Self, Self::Error> {
        match value {
            0 => Ok(DataStatus::Normal),
            1 => Ok(DataStatus::Update),
            2 => Ok(DataStatus::Insert),
            3 => Ok(DataStatus::Delete),
            4 => Ok(DataStatus::Child),
            other @ _ => Err(ErrorCode::RequestInvalid(format!(
                "can not deserizlize dataStatus for {:?}",
                other
            ))),
        }
    }
}

// impl<'de> serde::Deserialize<'de> for DataStatus {
//     fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
//     where
//         D: serde::Deserializer<'de>,
//     {
//         let data_status = match i32::deserialize(deserializer)? {
//             0 => DataStatus::Normal,
//             1 => DataStatus::Update,
//             2 => DataStatus::Insert,
//             3 => DataStatus::Delete,
//             4 => DataStatus::Child,
//             other @ _ => {
//                 return Err(serde::de::Error::custom(format!(
//                     "Invalid DataStatus '{}'",
//                     other
//                 )))
//             }
//         };
//         Ok(data_status)
//     }
// }
